package com.onelap.bls.tc.utils;

import android.app.Activity;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.PermissionUtils;
import com.blankj.utilcode.util.ScreenUtils;

import java.util.List;

public class PermissionUtil {

    /**
     * 申请权限
     *
     * @param permissions 权限
     * @param isDoIt      是否进行下一步操作
     */
    public static void applyForPermission(final String[] permissions, final boolean isDoIt, final HasPermission hasPermission) {
        PermissionUtils.permission(permissions)
                .rationale(new PermissionUtils.OnRationaleListener() {
                    @Override
                    public void rationale(final ShouldRequest shouldRequest) {
                        DialogHelper.showRationaleDialog(shouldRequest);
                    }
                })
                .callback(new PermissionUtils.FullCallback() {
                    @Override
                    public void onGranted(List<String> permissionsGranted) {
                        if (isDoIt)
                            hasPermission.hasPermission();
                    }

                    @Override
                    public void onDenied(List<String> permissionsDeniedForever, List<String> permissionsDenied) {
                        if (!permissionsDeniedForever.isEmpty())
                            DialogHelper.showOpenAppSettingDialog();
                        else PermissionUtil.applyForPermission(permissions, isDoIt, hasPermission);
                    }
                })
                .theme(new PermissionUtils.ThemeCallback() {
                    @Override
                    public void onActivityCreate(Activity activity) {
                        ScreenUtils.setFullScreen(activity);
                    }
                })
                .request();
    }


    public interface HasPermission {
        void hasPermission();
    }
}
